package com.turnengine.client.global.game.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.global.game.bean.GameHostSerializer;
import com.turnengine.client.global.game.bean.IGameHost;
import java.io.IOException;

/**
 * The Get Game Host By Name Data Serializer.
 */
public class GetGameHostByNameDataSerializer extends AbstractCommandDataSerializer<IGameHost> implements IGetGameHostByNameDataSerializer {

	/**
	 * Creates a new Get Game Host By Name Data Serializer.
	 */
	public GetGameHostByNameDataSerializer() {
		super(GetGameHostByName.COMMAND_ID);
	}

	@Override
	public IGameHost readReturnValue(IDataReader reader) throws IOException {
		IGameHost value = new GameHostSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IGameHost value) throws IOException {
		new GameHostSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IGameHost> readRequest(IDataReader reader) throws IOException {

		// Read fields
		String name = reader.readString(true);
		int port = reader.readInt();

		// Create object
		GetGameHostByName object = new GetGameHostByName();
		object.setName(name);
		object.setPort(port);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IGameHost> argument) throws IOException {
		GetGameHostByName object = (GetGameHostByName) argument;

		// Get fields
		String name = object.getName();
		int port = object.getPort();

		// Write fields
		writer.writeString(name, true);
		writer.writeInt(port);
	}

	@Override
	public IExecutableCommandResponse<IGameHost> newResponse() {
		return new GetGameHostByNameResponse();
	}
}
